/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import sys.bean.PermissaoBean;
import sys.utils.ConexaoMySql;

/**
 *
 * @author Administrador
 */
public class PermissaoDao {

    public static void excluir(String codigo) throws SQLException {

        Connection con = ConexaoMySql.getConection();

        String sql = "delete from permissoes where CODIGO_USUARIO = ?";
        PreparedStatement stmt = con.prepareStatement(sql);

        stmt.setString(1, codigo);
        stmt.execute();

        stmt.close();
        con.close();
    }

    //Incluir  usuario
    public static void incluir(List<PermissaoBean> permissoes) throws SQLException {

        Connection con = ConexaoMySql.getConection();
        String sql = "INSERT INTO permissoes (CODIGO_USUARIO, TELA, PERMISSAO) VALUES (?, ?, ?)";
        PreparedStatement stmt = con.prepareStatement(sql);

        for (PermissaoBean p : permissoes) {
            stmt.setString(1, p.getUsuario().getCodigo());
            stmt.setString(2, p.getTela());
            stmt.setInt(3, p.getPermissao());
            stmt.execute();
        }

        stmt.close();
        con.close();
    }

    //retornando permissoes do usuario
    public static List<PermissaoBean> gerPermissoesUsuario(String usuario) throws SQLException {
        Connection con = ConexaoMySql.getConection();
        List<PermissaoBean> lista = new ArrayList<>();

        String sql = "SELECT p.CODIGO_PERMISSAO, p.TELA, p.PERMISSAO \n"
                + "FROM permissoes p \n"
                + "where p.CODIGO_USUARIO = '" + usuario + "'";

        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            PermissaoBean permissao = new PermissaoBean();

            permissao.setCodigo(rs.getInt("CODIGO_PERMISSAO"));
            permissao.setTela(rs.getString("TELA"));
            permissao.setPermissao(rs.getInt("PERMISSAO"));

            lista.add(permissao);

        }
        stmt.close();
        con.close();
        
        return lista;
    }

}
